VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "DAOGuia"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private mvarConnection As utilConexion
Public Property Set Connection(ByVal vData As utilConexion)
    Set mvarConnection = vData
End Property

'---------------------------------------------------------------------------------------
' Procedure : getLista
' DateTime  : 30/11/2011 21:18
' Author    :
' Purpose   : Obtiene una lista de guias
'---------------------------------------------------------------------------------------
'
Public Function getGuiaInfo(bparm As bguia) As bguia
Dim sql As String
Dim rst As New Recordset
Dim record As bguia

sql = " SELECT ncodigo, sdocreferencia, dfecproceso, ntipoguia, stipoguia,  nestado, sestado, scomentarios,"
sql = sql + " susucreacion , dfeccreacion, susuultedicion, dfecultedicion "
sql = sql + "   FROM proc_guia_info(" + CStr(bparm.nCodigo) + ");"
rst.Open sql, mvarConnection.Connection, adOpenForwardOnly
If Not rst.EOF Then
    Set record = New bguia
    If Not IsNull(rst.Fields("ncodigo")) Then record.nCodigo = rst.Fields("ncodigo").value
    If Not IsNull(rst.Fields("sDocReferencia")) Then record.sDocReferencia = rst.Fields("sDocReferencia").value
    If Not IsNull(rst.Fields("dfecproceso")) Then record.dFecProceso = rst.Fields("dfecproceso").value
    If Not IsNull(rst.Fields("ntipoguia")) Then record.bTipoGuia.nCodigo = rst.Fields("ntipoguia").value
    If Not IsNull(rst.Fields("stipoguia")) Then record.bTipoGuia.sdescripcion = rst.Fields("stipoguia").value
    If Not IsNull(rst.Fields("nestado")) Then record.bestado.nCodigo = rst.Fields("nestado").value
    If Not IsNull(rst.Fields("sestado")) Then record.bestado.sdescripcion = rst.Fields("sestado").value
    If Not IsNull(rst.Fields("scomentarios")) Then record.sComentarios = rst.Fields("scomentarios").value
    If Not IsNull(rst.Fields("susucreacion")) Then record.bUsuarioCreacion.sNombre = rst.Fields("susucreacion").value
    If Not IsNull(rst.Fields("dfeccreacion")) Then record.dFecRegistro = rst.Fields("dfeccreacion").value
    If Not IsNull(rst.Fields("susuultedicion")) Then record.bUsuarioModificacion.sNombre = rst.Fields("susuultedicion").value
    If Not IsNull(rst.Fields("dfecultedicion")) Then record.dFecUltimaModificacion = rst.Fields("dfecultedicion").value
End If
Set getGuiaInfo = record
End Function

'---------------------------------------------------------------------------------------
' Procedure : getLista
' DateTime  : 30/11/2011 21:18
' Author    :
' Purpose   : Obtiene una lista de guias
'---------------------------------------------------------------------------------------
'
Public Function getLista(bparm As bguia) As Collection
Dim sql As String
Dim rst As New Recordset
Dim list As New Collection
Dim record As New bguia
Dim i As Integer
Dim sfecha As String
If bparm.dFecRegistro = UtilConstantes.FECHA_NULA Then
    sfecha = ""
End If
sql = " SELECT ncodigo, sdocreferencia, dfecproceso, ntipoguia, stipoguia,  nestado, sestado, scomentarios,"
sql = sql + " susucreacion , dfeccreacion, susuultedicion, dfecultedicion "
sql = sql + "   FROM proc_guia_lista(" + CStr(bparm.nCodigo) + "," + CStr(bparm.bTipoGuia.nCodigo)
sql = sql + "," + CStr(bparm.bestado.nCodigo) + ",'" + Format(sfecha, bParametro.sFormatoFecha) + "');"
rst.Open sql, mvarConnection.Connection, adOpenForwardOnly
While Not rst.EOF And i < UtilConstantes.MAX_RECORDS
    Set record = New bguia
    If Not IsNull(rst.Fields("ncodigo")) Then record.nCodigo = rst.Fields("ncodigo").value
    If Not IsNull(rst.Fields("sDocReferencia")) Then record.sDocReferencia = rst.Fields("sDocReferencia").value
    If Not IsNull(rst.Fields("dfecproceso")) Then record.dFecProceso = rst.Fields("dfecproceso").value
    If Not IsNull(rst.Fields("ntipoguia")) Then record.bTipoGuia.nCodigo = rst.Fields("ntipoguia").value
    If Not IsNull(rst.Fields("stipoguia")) Then record.bTipoGuia.sdescripcion = rst.Fields("stipoguia").value
    If Not IsNull(rst.Fields("nestado")) Then record.bestado.nCodigo = rst.Fields("nestado").value
    If Not IsNull(rst.Fields("sestado")) Then record.bestado.sdescripcion = rst.Fields("sestado").value
    If Not IsNull(rst.Fields("scomentarios")) Then record.sComentarios = rst.Fields("scomentarios").value
    If Not IsNull(rst.Fields("susucreacion")) Then record.bUsuarioCreacion.sNombre = rst.Fields("susucreacion").value
    If Not IsNull(rst.Fields("dfeccreacion")) Then record.dFecRegistro = rst.Fields("dfeccreacion").value
    If Not IsNull(rst.Fields("susuultedicion")) Then record.bUsuarioModificacion.sNombre = rst.Fields("susuultedicion").value
    If Not IsNull(rst.Fields("dfecultedicion")) Then record.dFecUltimaModificacion = rst.Fields("dfecultedicion").value 'Else record.dFecUltimaModificacion = rst.Fields("dfeccreacion").value
    rst.MoveNext
    list.Add record
    i = i + 1
Wend
Set getLista = list
End Function




'---------------------------------------------------------------------------------------
' Procedure : getInfo
' DateTime  : 30/11/2011 21:18
' Author    :
' Purpose   : Obtiene una guia
'---------------------------------------------------------------------------------------
'
Public Function getInfo(bparm As bguia) As bguia
Dim sql As String
Dim rst As New Recordset
Dim record As New bguia
sql = " SELECT ncodigo, sdocreferencia, dfecproceso, ntipoguia, stipoguia,  nestado, sestado, scomentarios,"
sql = sql + " susucreacion , dfeccreacion, susuultedicion, dfecultedicion "
sql = sql + "   FROM proc_guia_info(" + CStr(bparm.nCodigo) + ");"
rst.Open sql, mvarConnection.Connection, adOpenForwardOnly
If Not rst.EOF Then
    Set record = New bguia
    If Not IsNull(rst.Fields("ncodigo")) Then record.nCodigo = rst.Fields("ncodigo").value
    If Not IsNull(rst.Fields("sDocReferencia")) Then record.sDocReferencia = rst.Fields("sDocReferencia").value
    If Not IsNull(rst.Fields("dfecproceso")) Then record.dFecProceso = rst.Fields("dfecproceso").value
    If Not IsNull(rst.Fields("ntipoguia")) Then record.bTipoGuia.nCodigo = rst.Fields("ntipoguia").value
    If Not IsNull(rst.Fields("stipoguia")) Then record.bTipoGuia.sdescripcion = rst.Fields("stipoguia").value
    If Not IsNull(rst.Fields("nestado")) Then record.bestado.nCodigo = rst.Fields("nestado").value
    If Not IsNull(rst.Fields("sestado")) Then record.bestado.sdescripcion = rst.Fields("sestado").value
    If Not IsNull(rst.Fields("scomentarios")) Then record.sComentarios = rst.Fields("scomentarios").value
    If Not IsNull(rst.Fields("susucreacion")) Then record.bUsuarioCreacion.sNombre = rst.Fields("susucreacion").value
    If Not IsNull(rst.Fields("dfeccreacion")) Then record.dFecRegistro = rst.Fields("dfeccreacion").value
    If Not IsNull(rst.Fields("susuultedicion")) Then record.bUsuarioModificacion.sNombre = rst.Fields("susuultedicion").value
    If Not IsNull(rst.Fields("dfecultedicion")) Then record.dFecUltimaModificacion = rst.Fields("dfecultedicion").value
End If
Set getInfo = record
End Function

'---------------------------------------------------------------------------------------
' Procedure : insertar
' DateTime  : 23/04/2008 23:47
' Author    :
' Purpose   : inserta la informacion de una guia
'---------------------------------------------------------------------------------------
'
Public Function insertar(ByRef bean As bguia) As Integer
On Error GoTo hnderr
Dim rst As Recordset

Dim sql As String
sql = "select * from proc_guia_inserta('" + bean.bUsuarioCreacion.sNombre + "'," + CStr(bean.bTipoGuia.nCodigo) + ","
sql = sql + CStr(bean.bestado.nCodigo) + ",'" + bean.sDocReferencia + "','" + bean.sComentarios + "')"
Set rst = mvarConnection.Connection.Execute(sql)
If Not (rst.EOF And rst.BOF) Then
    If rst.Fields(0).value > 0 Then
        bean.nCodigo = rst.Fields(0).value
    Else
        insertar = UtilConstantes.OPT_ERROR_ENCONTRADO
    End If
Else
    insertar = UtilConstantes.OPT_ERROR_ENCONTRADO
End If
insertar = UtilConstantes.OPT_DATOS_GUARDADOS
hnderr:
If Err.Number = 0 Then Exit Function
    insertar = UtilConstantes.OPT_ERROR_ENCONTRADO
End Function


'---------------------------------------------------------------------------------------
' Procedure : actualizar
' DateTime  : 23/04/2008 23:47
' Author    :
' Purpose   : actualiza la informacion de una guia
'---------------------------------------------------------------------------------------
'
Public Function actualizar(bean As bguia) As Integer
On Error GoTo hnderr
Dim sql As String
sql = "select * from proc_guia_actualiza(" + CStr(bean.nCodigo) + ",'" + bean.bUsuarioModificacion.sNombre + "'," + CStr(bean.bTipoGuia.nCodigo) + ",'"
sql = sql + bean.sDocReferencia + "','" + bean.sComentarios + "')"
mvarConnection.Connection.Execute sql
actualizar = UtilConstantes.OPT_DATOS_GUARDADOS
hnderr:
If Err.Number = 0 Then Exit Function
    actualizar = UtilConstantes.OPT_ERROR_ENCONTRADO
End Function
Public Function guardaDetalle(bguia As bguia) As Integer
'On Error GoTo hnderr
Dim sql As String
Dim tb As New Recordset
Dim i As Integer
Dim bean As BGuiaDetalle
For Each bean In bguia.lDetalle
    sql = "select * from proc_guiadetalle_guarda(" + CStr(bean.nCodigo) + ","
    sql = sql + CStr(bean.bproducto.nCodigo) + "," + CStr(bean.bAlmDestino.nCodigo) + ","
    sql = sql + CStr(bean.nCantidad) + "," + CStr(bguia.nCodigo) + ")"
    Set tb = mvarConnection.Connection.Execute(sql)
    bean.nCodigo = tb.Fields(0).value
    bean.nEstado = UtilConstantes.ESTADO_DETALLE_GUARDADO
Next
    guardaDetalle = UtilConstantes.OPT_DATOS_GUARDADOS
hnderr:
    If Err.Number = 0 Then Exit Function
    guardaDetalle = UtilConstantes.OPT_ERROR_ENCONTRADO
End Function

'---------------------------------------------------------------------------------------
' Procedure : eliminarDetalle
' DateTime  : 22/04/2008 21:56
' Author    : Franco
' Purpose   : Elimina un item del pedido
'---------------------------------------------------------------------------------------
'
Public Function eliminarDetalle(bean As BGuiaDetalle) As Integer
'On Error GoTo hnderr
'Dim sql As String
'    If bean.nEstado = UtilConstantes.ESTADO_DETALLE_GUARDADO Then
'        sql = "DELETE FROM guiadetalle WHERE ncodigo=" + CStr(bean.nCodigo) & " and nguia=" + CStr(bean.bguia)
'        mvarConnection.Connection.Execute sql
'    End If
'    eliminarDetalle = UtilConstantes.OPT_DATOS_ELIMINADOS
'hnderr:
'    If Err.Number = 0 Then Exit Function
'    eliminarDetalle = UtilConstantes.OPT_ERROR_ENCONTRADO
    'Err.Clear
End Function
Public Function getListaDetalle(ByRef bean As bguia) As Integer
Dim sql As String
Dim rst As New Recordset
Dim list As New Collection
Dim record As New BGuiaDetalle
sql = "SELECT ncodigo , nproducto , sproducto , nalmacen, salmacen, nguia, ncantidad "
sql = sql + " FROM proc_guiadetalle_lista("
sql = sql + CStr(bean.nCodigo) + ")"
rst.Open sql, mvarConnection.Connection, adOpenForwardOnly
While Not rst.EOF
    Set record = New BGuiaDetalle
    If Not IsNull(rst.Fields("ncodigo")) Then record.nCodigo = rst.Fields("ncodigo").value
    If Not IsNull(rst.Fields("nproducto")) Then record.bproducto.nCodigo = rst.Fields("nproducto").value
    If Not IsNull(rst.Fields("sproducto")) Then record.bproducto.sDescripcionCorta = rst.Fields("sproducto").value
    If Not IsNull(rst.Fields("nalmacen")) Then record.bAlmDestino.nCodigo = rst.Fields("nalmacen").value
    If Not IsNull(rst.Fields("salmacen")) Then record.bAlmDestino.sDescripcionCorta = rst.Fields("salmacen").value
    If Not IsNull(rst.Fields("ncantidad")) Then record.nCantidad = rst.Fields("ncantidad").value
    record.nEstado = UtilConstantes.ESTADO_DETALLE_GUARDADO
    rst.MoveNext
    list.Add record
Wend
Set bean.lDetalle = list
End Function
Public Function procesar(ByRef bean As bguia) As Integer
Dim sql As String
Dim rst As New Recordset
Dim list As New Collection
Dim record As New BGuiaDetalle
sql = "SELECT * FROM proc_guia_procesar(" + CStr(bean.nCodigo) + ")"
rst.Open sql, mvarConnection.Connection, adOpenForwardOnly
If rst.Fields(0) = 0 Then
    procesar = UtilConstantes.OPT_DATOS_GUARDADOS
Else
    procesar = UtilConstantes.OPT_ERROR_ENCONTRADO
End If
rst.Close
End Function
